package com.then.component.login.ui.register;


import android.arch.lifecycle.ViewModelProviders;

import android.content.Intent;
import android.databinding.Observable;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.View;


import com.luojilab.component.componentlib.router.ui.UIRouter;
import com.luojilab.router.facade.annotation.Autowired;
import com.luojilab.router.facade.annotation.RouteNode;
import com.then.architecture.Country;
import com.then.component.login.R;
import com.then.component.login.ui.base.LoginModuleActivity;
import javax.inject.Inject;
import timber.log.Timber;


@RouteNode(path = "/registerPage")
public class RegisterActivity extends LoginModuleActivity<RegisterDelegate> {
    private static final int REQUEST_COMPLETE_PROFILE = 100;


    @Autowired
    boolean isRegister;

    private RegisterActivityViewModel registerActivityViewModel;

    private String countryCode;

    @Inject
    Country country;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerActivityViewModel = ViewModelProviders.of(this, getViewModelFactory()).get(RegisterActivityViewModel.class);
    }


    @Override
    protected void initData() {
        super.initData();
        countryCode = country.getDefaultCountryInfo().getCode();
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.setOnClickListener(this, R.id.btn_to_register, R.id.tv_send_code);
    }


    @Override
    protected Class getDelegateClass() {
        return RegisterDelegate.class;
    }


    @Override
    public void actionViewModel(View view, Object o, int actionType) {
        int id = view.getId();
        if (id == R.id.tv_send_code) {
            String phoneNum = viewDelegate.getPhoneNum();
            registerActivityViewModel.isPhoneRegister(phoneNum, countryCode).observe(this, resource -> {
                        if (resource.data != null) {
                            boolean sendSms = (isRegister && resource.data.message.errorCode == 0) || (!isRegister && resource.data.message.errorCode == 9);
                            if (sendSms) {
                                registerActivityViewModel.sendSms(phoneNum).observe(this, resource1 ->
                                        Timber.d("resource:" + resource1));
                            } else {
                                viewDelegate.setErrorCode(resource.data.message.errorCode);
                            }
                        }
                    }
            );
        } else if (id == R.id.btn_to_register) {
            registerOrFindPwd();
        }
    }


    /**
     * 注册或找回密码
     */
    private void registerOrFindPwd() {
        String phoneNum = viewDelegate.getPhoneNum();
        String pwd = viewDelegate.getPwd();
        String captcha = viewDelegate.getCaptcha();
        if (verification(phoneNum, pwd, captcha)) {
            if (isRegister) {
                registerActivityViewModel.register(phoneNum, pwd, captcha, countryCode, "").observe(this, resource -> {
                            if (resource.data != null) {
                                toRegisterComplete(resource.data.authInfo.authCode);
                            }
                        }
                );
            } else {
                registerActivityViewModel.findPwd(countryCode, phoneNum, captcha).observe(this, resource -> {
                            if (resource.data != null) {
                                if (resource.data.message.errorCode != 0) {
                                    viewDelegate.setErrorCode(resource.data.message.errorCode);
                                    return;
                                }
                                registerActivityViewModel.completePhoneFindPwd(countryCode, phoneNum, resource.data.authInfo.authCode, pwd, pwd).observe(this, resource1 -> {
                                            if (resource1.data != null && !TextUtils.isEmpty(resource1.data.userInfo.bduss)) {
//                                                returnToLogin();
                                                toRegisterComplete("");
                                            }
                                        }
                                );
                            }
                        }
                );
            }
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_COMPLETE_PROFILE) {
            if (resultCode == 1) {
                returnToLogin();
            }
        }
    }

    /**
     * 返回登录
     */
    private void returnToLogin() {
        Intent intent = new Intent();
        intent.putExtra("countryCode", countryCode);
        intent.putExtra("phoneNum", viewDelegate.getPhoneNum());
        intent.putExtra("pwd", viewDelegate.getPwd());
        viewDelegate.toast("密码已重置成功");
        setResult(1, intent);
        finish();
    }


    /**
     * 完善注册
     *
     * @param authCode
     */
    private void toRegisterComplete(String authCode) {
        Bundle bundle = new Bundle();
        bundle.putString("countryCode", countryCode);
        bundle.putString("phoneNum", viewDelegate.getPhoneNum());
        bundle.putString("pwd", viewDelegate.getPwd());
        bundle.putString("authCode", authCode);
        UIRouter.getInstance().openUri(RegisterActivity.this, "DDComp://login/registerCompletePage", bundle, REQUEST_COMPLETE_PROFILE);
    }


    /**
     * 验证用户名和密码
     *
     * @param phoneNum
     * @param pwd
     * @return
     */
    private boolean verification(String phoneNum, String pwd, String captcha) {
        return true;
    }


}
